﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clandestine.Graphics
{
	public delegate void PreRenderEvent();
	
    public abstract partial class Renderable
    {
        protected Graphics graphics;
        private RenderList renderList;

		public bool Visible { get; set; }
		
		public event PreRenderEvent PreRender
		{
			add { graphics.PreRender += value; }
			remove { graphics.PreRender -= value; }
		}

		public Renderable(Graphics graphics) : this (graphics, graphics.DefaultRenderList) { }
		public Renderable(RenderList list) : this(list.Graphics, list) { }
        public Renderable(Graphics graphics, RenderList list)
        {
            this.graphics = graphics;
            this.renderList = list;
			this.Visible = true;

			// Force Depth search & RenderList add.
			this.Depth = decimal.Zero;
        }

        protected abstract void render();

		internal void Render()
		{
			if (Visible)
				render();
		}

        public virtual void Dispose()
        {
            graphics.BlockRendering();
            
            if (renderList.Contains(this))
                renderList.Remove(this);
                
          	graphics.UnblockRendering();
        }
    }
}
